import * as React from 'react'
import { RouteProps, Switch, Route, Redirect } from 'react-router-dom'
import Loading from '../components/Loading'
import NotFound from '../components/NotFound'

const { lazy, Suspense } = React

const Demo = lazy(() => import( /* webpackChunkName:"demo" */ '../pages/Demo/index'))

const routes: RouteProps[] = []

const $PROD = false
if (!$PROD) {
	routes.push({
		path: '/admin/demo',
		component: Demo
	})
}

routes.push({
	path: '*',
	component: NotFound
})

interface IProps {
	authorized: boolean	//是否有权限
}

export class AdminRoutes extends React.Component<IProps> {
	render() {
		const { authorized } = this.props

		return (
			<Suspense fallback={<Loading />}>
				<Switch>
					{
						routes.map(route => {
							const { path, exact, component: LazyCom } = route

							if (!LazyCom) return null
							return (
								<Route
									key={path + ''}
									exact={exact}
									path={path}
									component={LazyCom}
									render={props => authorized ? <LazyCom {...props} /> : <Redirect to="/" />}
								/>
							)
						})
					}
				</Switch>
			</Suspense>
		)
	}
}